﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace zielonka.co.uk.extensions.system.io
{
    // Description:	Convert a Stream to an array of bytes.
    /*
        FileStream f = File.OpenRead(@"c:\testfile.txt");
        byte[] b = f.ConvertToByteArray();
        Console.WriteLine(b.Length);
     */

    public static partial class StreamExtensions
    {
        public static byte[] ConvertToByteArray(this Stream stream)
        {
            var streamLength = Convert.ToInt32(stream.Length);
            var data = new byte[streamLength];

            stream.Read(data, 0, streamLength);
            stream.Close();

            return data;
        }
    }
}
